// page/pack-mine/pages/customer-help/customer-help.js
var httpUtils = require('../../../../utils/https-utils.js')
var toolutils = require("../../../../utils/tool-utils.js")
var that
var mPageSize = 10//每页条数
var mTotalNum //总条数
var mCurrentPage = 1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValues:'',
    checkId:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   that = this
   mCurrentPage = 1
   getQuestionList()
  },
  onQuestionItem:function(e){
    var index = e.currentTarget.dataset.index
    if (that.data.checkId == index){
      index = -1
    }
    that.setData({
      checkId:index
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (mTotalNum > mPageSize * mCurrentPage) {
      ++mCurrentPage
      that.setData({
        loadData: {
          searchLoading: true, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
        }
      })
      getQuestionList()
    } else {
      that.setData({
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: true  //“没有数据”的变量，默认false，隐藏  
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //用户反馈
  formSubmit:function(e){
    var inputValue = e.detail.value.inputValue
    if (!inputValue){
      toolutils.showFailToast("反馈内容不能为空")
      return
    }
    var params = {}
    params.feedback_content = inputValue
    var StreetInfo = getApp().getStreetInfo()
    if (StreetInfo && StreetInfo.group_id) {
      params.group_id = StreetInfo.group_id
    }else{
      params.group_id = 1
    }
    httpUtils.putFeedBack(params,function(res){
      toolutils.showFailToast("提交成功，感谢您的反馈")
      that.setData({
        inputValues:''
      })
    },function(e){
      toolutils.showFailToast("提交失败")
    })
  }
})

function getQuestionList(){
  var params = {}
  params.current_page = mCurrentPage
  params.page_size = mPageSize
  var StreetInfo = getApp().getStreetInfo()
  if (StreetInfo && StreetInfo.group_id) {
    params.group_id = StreetInfo.group_id
  }
  httpUtils.getQuestionList(params,function(res){
    mTotalNum = res.total_num
    var list = that.data.dataList
    if (mCurrentPage == 1) {
      list = []
    }
    list = list.concat(res.list)
    if (list.length == mTotalNum) {
      that.setData({
        dataList: list,
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: true  //“没有数据”的变量，默认false，隐藏  
        }
      })
    } else {
      that.setData({
        dataList: list,
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
        }
      })
    }
    wx.stopPullDownRefresh()
  }, function (e) {
    if (mCurrentPage > 1) {
      --mCurrentPage
    }
    that.setData({
      loadData: {
        searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
        searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
      }
    })
    wx.stopPullDownRefresh()
  })
}